<?php
namespace Admin\Model;

use Think\Model;

class GuwenLevelModel extends Model
{

    protected $_auto = array(
        array('addtime', NOW_TIME, self::MODEL_INSERT)
    );
    public function get_level_list($where,$offset=10){

        $count=$this->where($where)->count();

        $pager = pager($count, $offset);//实例化thinkphp内置的分页显示类
        $result = $this->where($where)->order('id desc')->limit($pager->firstRow,$pager->listRows)->select();
        return $result;
    }

    /**
     * 获取用户待审核顾问回调
     * @return mixed
     */

    public static function get_answer_list()
    {
        $count=M('questionnaire_user_list')->join('as q left join qs_members as m on q.uid=m.uid')->where(['q.status'=>0])->count();
        $pager = pager($count, 10);
        $list = M('questionnaire_user_list')
            ->field('q.*,m.username')
            ->join('as q left join qs_members as m on q.uid=m.uid')
            ->where(['q.status'=>0])->limit($pager->firstRow . ',' . $pager->listRows)->order('id desc')->select();
        return $list;

    }
    /**
     * 获取用户待审核顾问回调详情
     * @return mixed
     */
    public static function get_answer_detail($id)
    {
        $list = M('questionnaire_user_list')
            ->where(['status'=>0,'id'=>$id])->find();
        $list['content']=unserialize($list['content']);
        $list['username']=M('members')->where(['uid'=>$list['uid']])->getField('username');
        return $list;
    }

    /**
     * 审核顾问
     * @param $id
     * @param $status
     * @param $reson
     * @return bool
     */

    public static function save_answer_status($id,$status,$reson)
    {
       $res=M('questionnaire_user_list')->where(['id'=>$id])->save([
          'status'=>$status,
          'reson'=>$reson,
           'uptime'=>date('Y-m-d H:i:s',NOW_TIME)
       ]);
       $info=M('questionnaire_user_list')->where(['id'=>$id])->find();
       if($res ){
           if($status==1){
               /*M('members')->where(['uid'=>$info['uid']])->save([
                   'level'=>$info['level'],
                   'levelname'=>$info['level_name'],

               ]);*/
                //审核通过添加 入注记录
                M('gu_wen_log')->add([
                    'uid'   => $info['uid'],
                    'type'  => 2,
                    'note'  => '审核通过，入驻顾问',
                    'level' => $info['level'],
                    'level_name'=>$info['level_name'],
                    'add_time'=>date('Y-m-d h:i:s',NOW_TIME),
                ]);

               //推送微信小程序通知
               $membersBind    = M('MembersBind')->where(['uid'=>$info['uid']])->find();
               if(!empty($membersBind['openid'])){
                   $data=[
                       'thing5' => ['value' =>'恭喜！您的顾问资质审核已通过'],//温馨提示
                       'thing3' => ['value'  =>'审核通过'],//审核结果
                       'time4'  => ['value'   => $info['addtime']],//申请时间
                       'time7'  => ['value'   => date('Y-m-d H:i:s', time())],//审核时间
                   ];
                   wxMessageSend($membersBind['openid'],9187, $data);
               }
           }
           return true;
       }else{
           return false;
       }
    }
}